package com.ktjiaoyu.crm.service.impl;

import com.ktjiaoyu.crm.entity.Customer;
import com.ktjiaoyu.crm.entity.User;
import com.ktjiaoyu.crm.repository.CustomerRepository;
import com.ktjiaoyu.crm.repository.UserRepository;
import com.ktjiaoyu.crm.service.ICustomerService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;

/**
 * 实现类---客户
 * @author lee
 */
@Service(value = "customerService")
public class CustomerServiceImpl implements ICustomerService {
    @Resource
    private CustomerRepository customerRepository;

    @Resource
    private UserRepository userRepository;

    /**
     * 查询客户
     *
     * @param custName        客户名称
     * @param custNo          客户编号
     * @param custRegion      客户地区
     * @param custManagerName 客户负责人名称
     * @param custLevel       客户等级
     * @param pageable        分页条件
     * @return page
     */
    @Override
    public Page<Customer> findCustomers(String custName, String custNo,
                                        String custRegion, String custManagerName,
                                        Integer custLevel, Pageable pageable) {
        Specification<Customer> customerSpecification = new Specification<Customer>() {
            @Override
            public Predicate toPredicate(Root<Customer> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> predicates = new ArrayList<>();
                if (custName!=null && !custName.equals("")){
                    predicates.add(criteriaBuilder.like(root.get("custName"), "%"+custName+"%"));
                }
                if (custNo!=null && !custNo.equals("")){
                    predicates.add(criteriaBuilder.like(root.get("custNo"), "%"+custNo+"%"));
                }
                if (custRegion!=null && !custRegion.equals("")){
                    predicates.add(criteriaBuilder.like(root.get("custRegion"), "%"+custRegion+"%"));
                }
                if (custManagerName!=null && !custManagerName.equals("")){
                    predicates.add(criteriaBuilder.like(root.get("custManagerName"), "%"+custManagerName+"%"));
                }
                if (custLevel!=null && custLevel!=0){
                    predicates.add(criteriaBuilder.equal(root.get("custLevel"), custLevel));
                }
                predicates.add(criteriaBuilder.equal(root.get("custStatus"), "1"));
                return criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()]));
            }
        };
        return customerRepository.findAll(customerSpecification, pageable);
    }

    /**
     * 查找同一角色的用户
     *
     * @param roleId 角色Id
     * @return list
     */
    @Override
    public List<User> findUserByRoleId(Long roleId) {
        return this.userRepository.findUsersByRoleRoleId(roleId);
    }
}
